using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Collections;

namespace Common.Graphics
{
    public partial class LabelList : UserControl
    {

        public static int DIRECTION_NONE = -1;
        public static int DIRECTION_UP = 0;
        public static int DIRECTION_DOWN = 1;

        public int YDirection = DIRECTION_NONE;

        public int[] LabelPositions = new int[7] { 0, 40, 80, 120, 160, 200, 240 };
        public string[] ListData = new string[217] { "Example 1", "Example 2", "Example 3", "Example 4", "Example 5", "Example 6", "Example 7", "Example 8", "Example 9", "Example 10", "Example 11", "Example 12", "Example 13", "Example 14", "Example 15", "Example 16", "Example 17", "Example 18", "Example 19", "Example 20", "Example 21", "Example 22", "Example 23", "Example 24", "Example 25", "Example 26", "Example 27", "Example 28", "Example 29", "Example 30", "Example 31", "Example 32", "Example 33", "Example 34", "Example 35", "Example 36", "Example 37", "Example 38", "Example 39", "Example 40", "Example 41", "Example 42", "Example 43", "Example 44", "Example 45", "Example 46", "Example 47", "Example 48", "Example 49", "Example 50", "Example 51", "Example 52", "Example 53", "Example 54", "Example 55", "Example 56", "Example 57", "Example 58", "Example 59", "Example 60", "Example 61", "Example 62", "Example 63", "Example 64", "Example 65", "Example 66", "Example 67", "Example 68", "Example 69", "Example 70", "Example 71", "Example 72", "Example 73", "Example 74", "Example 75", "Example 76", "Example 77", "Example 78", "Example 79", "Example 80", "Example 81", "Example 82", "Example 83", "Example 84", "Example 85", "Example 86", "Example 87", "Example 88", "Example 89", "Example 90", "Example 91", "Example 92", "Example 93", "Example 94", "Example 95", "Example 96", "Example 97", "Example 98", "Example 99", "Example 100", "Example 101", "Example 102", "Example 103", "Example 104", "Example 105", "Example 106", "Example 107", "Example 108", "Example 109", "Example 110", "Example 111", "Example 112", "Example 113", "Example 114", "Example 115", "Example 116", "Example 117", "Example 118", "Example 119", "Example 120", "Example 121", "Example 122", "Example 123", "Example 124", "Example 125", "Example 126", "Example 127", "Example 128", "Example 129", "Example 130", "Example 131", "Example 132", "Example 133", "Example 134", "Example 135", "Example 136", "Example 137", "Example 138", "Example 139", "Example 140", "Example 141", "Example 142", "Example 143", "Example 144", "Example 145", "Example 146", "Example 147", "Example 148", "Example 149", "Example 150", "Example 151", "Example 152", "Example 153", "Example 154", "Example 155", "Example 156", "Example 157", "Example 158", "Example 159", "Example 160", "Example 161", "Example 162", "Example 163", "Example 164", "Example 165", "Example 166", "Example 167", "Example 168", "Example 169", "Example 170", "Example 171", "Example 172", "Example 173", "Example 174", "Example 175", "Example 176", "Example 177", "Example 178", "Example 179", "Example 180", "Example 181", "Example 182", "Example 183", "Example 184", "Example 185", "Example 186", "Example 187", "Example 188", "Example 189", "Example 190", "Example 191", "Example 192", "Example 193", "Example 194", "Example 195", "Example 196", "Example 197", "Example 198", "Example 199", "Example 200", "Example 201", "Example 202", "Example 203", "Example 204", "Example 205", "Example 206", "Example 207", "Example 208", "Example 209", "Example 210", "Example 211", "Example 212", "Example 213", "Example 214", "Example 215", "Example 216", "Example 217" };

        public ArrayList SelectedItems = new ArrayList();

        public bool Stop = true;
        public double Speed = 40;

        public int CurrentPosition = 0;

        public bool AllowPaint = false;
        public Bitmap BackScreen = null;

        public Thread LabelMoverThread;
        public LabelMover LabelMover = new LabelMover();        

        public LabelList()
        {
            InitializeComponent();

            SelectedItems.Add(10);
            LabelMover.List = this;
            LabelMover.OnRefresh += new EventHandler(LabelMover_OnRefresh);
            LabelMoverThread = new Thread(new ThreadStart(LabelMover.Start));
            LabelMoverThread.IsBackground = true;
            LabelMoverThread.Priority = ThreadPriority.Lowest;
            LabelMoverThread.Start();
        }

        void LabelMover_OnRefresh(object sender, EventArgs e)
        {            
            this.Invalidate();
        }


        Brush LabelBrush1 = new SolidBrush(Color.White); //Brush LabelBrush1 = new SolidBrush(Color.FromArgb(20, 20, 20));
        Brush LabelBrush2 = new SolidBrush(Color.FromArgb(240, 240, 240)); //Brush LabelBrush2 = new SolidBrush(Color.Black);
        Brush SelectedBrush = new SolidBrush(Color.Blue); //Brush SelectedBrush = new SolidBrush(Color.LightSlateGray);

        Bitmap ScreenBuffer;
        protected override void OnPaint(PaintEventArgs e)
        {
            //ThreadStart thrdStart = new ThreadStart(Draw);
            //Thread thrd = new Thread(thrdStart);
            //thrd.Start();

            if (ScreenBuffer == null)
            {
                ScreenBuffer = new Bitmap(ClientSize.Width, ClientSize.Height);
            }

            Brush brush = null;
            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(ScreenBuffer);
            g.Clear(Color.White); // g.Clear(Color.Black);
            
            Brush background;

            for (int i = 0; i < LabelPositions.Length; i++) 
            {                

                if (((CurrentPosition + i) % 2) == 0)
                {
                    background = LabelBrush1;
                }
                else
                {
                    background = LabelBrush2;
                }

                if (SelectedItems.Contains((CurrentPosition + i)))
                {
                    g.FillRectangle(SelectedBrush, new Rectangle(0, LabelPositions[i] - 2, this.Width, 40));
                }
                else if (i == SelectedItem)
                {
                    Common.Graphics.Gradient.Fill(g, new Rectangle(0, LabelPositions[i] - 2, this.Width, 20), Color.White, Color.Blue, Gradient.FillDirection.TopToBottom);
                    //Common.Graphics.Gradient.Fill(g, new Rectangle(0, LabelPositions[i] - 2, this.Width, 20), Color.SlateGray, Color.DarkSlateGray, Gradient.FillDirection.TopToBottom);
                    g.FillRectangle(new SolidBrush(Color.DarkSlateGray), new Rectangle(0, LabelPositions[i] - 2 + 20, this.Width, 20));
                }
                else
                {
                    g.FillRectangle(background, new Rectangle(0, LabelPositions[i] - 2, this.Width, 40));
                }

                if ((i == SelectedItem) || SelectedItems.Contains((CurrentPosition + i)))
                {
                    brush = new SolidBrush(Color.White);
                }
                else
                {
                    brush = new SolidBrush(Color.Black); //brush = new SolidBrush(Color.SeaShell);
                }
                g.DrawString(ListData[CurrentPosition + i], this.Font, brush, 5, LabelPositions[i]);
            } 

            e.Graphics.DrawImage(ScreenBuffer, 0, 0);
            AllowPaint = false;

        }

        bool cleared = false;
        protected override void OnPaintBackground(PaintEventArgs e)
        {
            if (!cleared)
            {
                e.Graphics.Clear(Color.Black);
                cleared = true;
            }
        }

        long startClickTicks = 0;
        int startClickX = 0;
        int startClickY = 0;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            Stop = true;
            startClickTicks = Environment.TickCount;
            startClickX = e.X;
            startClickY = e.Y;
        }


        int SelectedItem = -1;

        int lastClicked = 0;

        protected override void OnMouseUp(MouseEventArgs e)
        {
            double ms = (double)(Environment.TickCount - startClickTicks);

            bool left = false;

            int xChange = 0;
            if (e.X > startClickX)
            {
                xChange = e.X - startClickX;
                left = false;
            }
            else
            {
                xChange = startClickX - e.X;
                if (xChange > 50)
                {
                    left = true;
                }
            }

            int yChange = 0;
            if (e.Y > startClickY)
            {
                yChange = e.Y - startClickY;
                YDirection = DIRECTION_DOWN;
            }
            else
            {
                yChange = startClickY - e.Y;
                YDirection = DIRECTION_UP;
            }


            if (left)
            {
                System.Drawing.Graphics g = this.CreateGraphics();
                /*for (int i = 240; i >= 0; i--)
                {
                    Thread.Sleep(i / 100);
                    g.FillRectangle(SelectedBrush, new Rectangle(i, 0, (240 - i), 240));
                } */
                for (int i = 0; i <= 240; i += 5)
                {
                    //Thread.Sleep(i / 50);
                    //g.FillRectangle(SelectedBrush, new Rectangle(i, 0, 1, 240));
                    Common.Graphics.Gradient.Fill(g, new Rectangle(0, 0, i, 240), Color.Black, Color.DarkSlateGray, Gradient.FillDirection.LeftToRight);
                }
                LabelPositions = new int[7] { 0, 40, 80, 120, 160, 200, 240 };
                CurrentPosition = 0;
                lastClicked = -1;
                AllowPaint = true;
                this.Refresh();
            }
            else
            {

                SelectedItem = -1;
                if ((yChange + xChange) < 20)
                {
                    Stop = true;

                    for (int i = 0; i < LabelPositions.Length; i++)
                    {
                        if (e.Y < (LabelPositions[i] + 40))
                        {
                            if (lastClicked == (CurrentPosition + i))
                            {
                                if (!SelectedItems.Contains((CurrentPosition + i)))
                                {
                                    SelectedItems.Add(CurrentPosition + i);
                                }
                                else
                                {
                                    SelectedItems.Remove(CurrentPosition + i);
                                }
                                lastClicked = -1;
                                AllowPaint = true;
                                this.Refresh();
                                break;
                            }
                            else
                            {
                                lastClicked = (CurrentPosition + i);
                                SelectedItem = i;
                                AllowPaint = true;
                                this.Refresh();
                                break;
                            }
                        }
                    }

                }
                else
                {
                    Speed = (yChange * 100) / ms;
                    Stop = false;
                }

            }

        }

    }
}
